package com.xiaomo.gitee;

import org.ho.yaml.Yaml;
import org.meowy.cqp.jcq.entity.*;
import org.meowy.cqp.jcq.event.JcqAppAbstract;

import java.io.*;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class bot extends JcqAppAbstract implements ICQVer, IMsg, IRequest {

    public static bot instance;
    public String directory = CQ.getAppDirectory();
    public long masterQQ = 1934944134L;
    public static boolean Enable = false;
    public Admin AdminConfig;
    public GroupList groupList;
    public BlackList blackList;
    public StringBuffer menuStringBuffer;
    public menuList menulist;
    public Word word;
    public StringBuffer runCode;

    public bot() {
    }

    public bot(CoolQ CQ) {
        super(CQ);
    }

    @Override
    public int startup() {
        CQ.sendPrivateMsg(masterQQ, "机器人启动中...");
        setInstance(this);
        CQ.sendPrivateMsg(masterQQ, "正在检查配置文件...");
        reloadConfig();
        groupList.getGrouplist();
        CQ.sendPrivateMsg(masterQQ, "配置文件读取完毕");
        CQ.sendPrivateMsg(masterQQ, "机器人已启动");
        return 0;
    }

    @Override
    public String appInfo() {
        return CQAPIVER + ",com.xiaomo.gitee.bot";
    }

    //退出
    @Override
    public int exit() {
        return 0;
    }

    //启用
    @Override
    public int enable() {
        return 0;
    }

    //卸载
    @Override
    public int disable() {
        return 0;
    }

    //私聊消息
    @Override
    public int privateMsg(int subType, int msgId, long fromQQ, String msg, int font) {
        if (msg.equals("开机")) {
            if (fromQQ != masterQQ) {
                CQ.sendPrivateMsg(fromQQ, "你没有权限这样做");
                return 0;
            }
            if (Enable == true) {
                CQ.sendPrivateMsg(fromQQ, "机器人已经是开机状态了！");
                return 0;
            } else {
                Enable = true;
                CQ.sendPrivateMsg(fromQQ, "开机成功");
                return 0;
            }
        }
        if (msg.equals("关机")) {
            if (fromQQ != masterQQ) {
                CQ.sendPrivateMsg(fromQQ, "你没有权限这样做");
                return 0;
            }
            if (Enable == false) {
                CQ.sendPrivateMsg(fromQQ, "机器人已经是关机状态了！");
                return 0;
            } else {
                Enable = false;
                CQ.sendPrivateMsg(fromQQ, "关机成功");
                return 0;
            }
        }
        if (msg.equals("状态")) {
            if (fromQQ != masterQQ) {
                CQ.sendPrivateMsg(fromQQ, "你没有权限这样做");
                return 0;
            }
            if (Enable == true) {
                CQ.sendPrivateMsg(fromQQ, "目前状态: 开机");
                return 0;
            } else {
                CQ.sendPrivateMsg(fromQQ, "目前状态: 关机");
                return 0;
            }
        }
        return 0;
    }

    //群消息
    @Override
    public int groupMsg(int subType, int msgId, long fromGroup, long fromQQ, String fromAnonymous, String msg, int font) {
        if (Enable == false) {
            return 0;
        }
        String[] str = msg.split("\\s+");
        if (str.length == 0) {
            return 0;
        }
        if (msg.equals("/bot reload")) {
            if (fromQQ == masterQQ) {
                CQ.sendGroupMsg(fromGroup, "正在重载配置文件，请稍后...");
                reloadConfig();
                CQ.sendGroupMsg(fromGroup, "重载配置文件完成");
                return 0;
            }
        }
        if (groupList.getGrouplist().contains(fromGroup)) {
            if (!(AdminConfig.getAdminList().contains(fromQQ) || fromQQ == masterQQ)) {
                String tmsg = msg.replaceAll("\\s*", "");
                for (String s : word.getWord()) {
                    if (tmsg.contains(s)) {
                        CQ.deleteMsg(msgId);
                        CQ.sendGroupMsg(fromGroup, CC.at(fromQQ) + " 你发送了违禁词，已帮你撤回");
                        return 0;
                    }
                }
            }
        }
        if (str[0].equals("/group")) {
            if (str.length == 2) {
                if (str[1].equals("add")) {
                    if (fromQQ == masterQQ) {
                        if (!(groupList.getGrouplist().contains(fromGroup))) {
                            List<Long> list = groupList.getGrouplist();
                            list.add(fromGroup);
                            groupList.setGrouplist(list);
                            try {
                                Yaml.dump(groupList, new File(CQ.getAppDirectory(), "grouplist.yml"));
                            } catch (FileNotFoundException e) {
                                CQ.sendPrivateMsg(masterQQ, "发生异常: FileNotFoundException \r\n详细信息: " + e.getMessage());
                            }
                            CQ.sendGroupMsg(fromGroup, "已成功授权此群,群号: " + String.valueOf(fromGroup));
                            return 0;
                        } else {
                            CQ.sendGroupMsg(fromGroup, "此群已经被授权了");
                            return 0;
                        }
                    } else {
                        CQ.sendGroupMsg(fromGroup, "你没有权限这样做");
                        return 0;
                    }
                }
                if (str[1].equals("remove")) {
                    if (fromQQ == masterQQ) {
                        if (groupList.getGrouplist().contains(fromGroup)) {
                            List<Long> list = groupList.getGrouplist();
                            for (int i = 0; i < list.size(); i++) {
                                if (list.get(i).equals(fromGroup)) {
                                    list.remove(i);
                                    break;
                                }
                            }
                            groupList.setGrouplist(list);
                            CQ.sendGroupMsg(fromGroup, "已成功取消此群的授权");
                            return 0;
                        } else {
                            CQ.sendGroupMsg(fromGroup, "这群本来就没有授权！");
                            return 0;
                        }
                    } else {
                        CQ.sendGroupMsg(fromGroup, "你没有权限这样做");
                        return 0;
                    }
                }
                if (str[1].equals("leave")) {
                    if (fromQQ == masterQQ) {
                        CQ.sendGroupMsg(fromGroup, "机器人已退出该群");
                        CQ.setGroupLeave(fromGroup, false);
                        return 0;
                    } else {
                        CQ.sendGroupMsg(fromGroup, "你没有权限这样做");
                        return 0;
                    }
                }
            }
        }
        if (groupList.getGrouplist().contains(fromGroup)) {
            if (str[0].equals("菜单") && str.length == 1) {
                CQ.sendGroupMsg(fromGroup, menuStringBuffer.toString());
                return 0;
            }
            if (str[0].equals("/bot") && str.length == 1) {
                CQ.sendGroupMsg(fromGroup, menuStringBuffer.toString());
                return 0;
            }
            if (str[0].equals("/bot") && str[1].equals("群管") && str.length == 2) {
                CQ.sendGroupMsg(fromGroup, loadMenu.GroupManager.toString());
                return 0;
            }
            switch (str[0]) {
                case "/kick":
                    if (AdminConfig.getAdminList().contains(fromQQ) || fromQQ == masterQQ) {
                        if (str.length == 2) {
                            if (str[1].matches("[0-9]+")) {
                                if (hasUser(fromGroup, Long.parseLong(str[1]))) {
                                    CQ.setGroupKick(fromGroup, Long.parseLong(str[1]), false);
                                    CQ.sendGroupMsg(fromGroup, "已成功踢出用户: " + str[1]);
                                    return 0;
                                } else {
                                    CQ.sendGroupMsg(fromGroup, "未找到此用户，请检查QQ号是否输入正确");
                                    return 0;
                                }
                            } else {
                                switch (String.valueOf(CC.getAt(str[1]))) {
                                    case "-1":
                                        CQ.sendGroupMsg(fromGroup, "不能踢出所有人");
                                        return 0;
                                    case "-1000":
                                        CQ.sendGroupMsg(fromGroup, "无效信息，请输入QQ号或 @某人");
                                        return 0;
                                    default:
                                        CQ.setGroupKick(fromGroup, CC.getAt(str[1]), false);
                                        CQ.sendGroupMsg(fromGroup, "已成功踢出用户: " + String.valueOf(CC.getAt(str[1])));
                                        return 0;
                                }
                            }
                        } else {
                            CQ.sendGroupMsg(fromGroup, "参数错误，正确用法: /kick [用户]");
                            return 0;
                        }
                    } else {
                        CQ.sendGroupMsg(fromGroup, "你没有权限这样做");
                        return 0;
                    }
                case "/mute":
                    if (AdminConfig.getAdminList().contains(fromQQ) || fromQQ == masterQQ) {
                        if (str.length == 3) {
                            if (str[2].matches("[0-9]+")) {
                                if ((!(Long.parseLong(str[2]) > 720) && Long.parseLong(str[2]) != 0)) {
                                    long time = Long.parseLong(str[2]) * 3600;
                                    if (str[1].matches("[0-9]+")) {
                                        if (hasUser(fromGroup, Long.parseLong(str[1]))) {
                                            CQ.setGroupBan(fromGroup, Long.parseLong(str[1]), time);
                                            CQ.sendGroupMsg(fromGroup, "成功禁言用户: " + CC.at(CC.getAt(str[1])));
                                            return 0;
                                        } else {
                                            CQ.sendGroupMsg(fromGroup, "未找到此用户");
                                            return 0;
                                        }
                                    } else {
                                        switch (String.valueOf(CC.getAt(str[1]))) {
                                            case "-1":
                                                CQ.sendGroupMsg(fromGroup, "全体禁言请使用 /muteall");
                                                return 0;
                                            case "-1000":
                                                CQ.sendGroupMsg(fromGroup, "无效信息，请输入QQ号或 @某人");
                                                return 0;
                                            default:
                                                CQ.setGroupBan(fromGroup, CC.getAt(str[1]), time);
                                                CQ.sendGroupMsg(fromGroup, "已成功禁言用户: " + CC.at(CC.getAt(str[1])) + " " + str[2] + "小时");
                                                return 0;
                                        }
                                    }
                                } else {
                                    CQ.sendGroupMsg(fromGroup, "时长不能超过720,而且不能为0");
                                    return 0;
                                }
                            } else {
                                CQ.sendGroupMsg(fromGroup, "请正确的输入整数时长");
                                return 0;
                            }
                        } else {
                            CQ.sendGroupMsg(fromGroup, "参数错误,正确用法: /mute [用户] [时间]  单位:小时");
                            return 0;
                        }
                    } else {
                        CQ.sendGroupMsg(fromGroup, "你没有权限这样做");
                        return 0;
                    }
                case "/unmute":
                    if (AdminConfig.getAdminList().contains(fromQQ) || fromQQ == masterQQ) {
                        if (str.length == 2) {
                            if (str[1].matches("[0-9]+")) {
                                if (hasUser(fromGroup, Long.parseLong(str[1]))) {
                                    CQ.setGroupBan(fromGroup, Long.valueOf(str[1]), 0);
                                    CQ.sendGroupMsg(fromGroup, "成功解除禁言用户: " + CC.at(CC.getAt(str[1])));
                                    return 0;
                                } else {
                                    CQ.sendGroupMsg(fromGroup, "没有找到此用户");
                                    return 0;
                                }
                            } else {
                                switch (String.valueOf(CC.getAt(str[1]))) {
                                    case "-1":
                                        CQ.setGroupWholeBan(fromGroup, false);
                                        CQ.sendGroupMsg(fromGroup, "已成功解除禁言全体用户");
                                        return 0;
                                    case "-1000":
                                        CQ.sendGroupMsg(fromGroup, "无效信息，请输入QQ号或 @某人");
                                        return 0;
                                    default:
                                        CQ.setGroupBan(fromGroup, CC.getAt(str[1]), 0);
                                        CQ.sendGroupMsg(fromGroup, "已成功解除禁言用户: " + CC.at(CC.getAt(str[1])));
                                        return 0;
                                }
                            }
                        } else {
                            CQ.sendGroupMsg(fromGroup, "参数错误,正确用法: /unmute [用户/全体用户]");
                            return 0;
                        }
                    } else {
                        CQ.sendGroupMsg(fromGroup, "你没有权限这样做");
                        return 0;
                    }
                case "/unmuteall":
                    if (AdminConfig.getAdminList().contains(fromQQ) || fromQQ == masterQQ) {
                        if (str.length == 1) {
                            CQ.setGroupWholeBan(fromGroup, false);
                            CQ.sendGroupMsg(fromGroup, "成功解除禁言全体成员");
                            return 0;
                        } else {
                            CQ.sendGroupMsg(fromGroup, "参数错误，正确用法: /unmuteall");
                            return 0;
                        }
                    } else {
                        CQ.sendGroupMsg(fromGroup, "你没有权限这样做");
                        return 0;
                    }
                case "/muteall":
                    if (AdminConfig.getAdminList().contains(fromQQ) || fromQQ == masterQQ) {
                        if (str.length == 1) {
                            CQ.setGroupWholeBan(fromGroup, true);
                            CQ.sendGroupMsg(fromGroup, "成功开启全体禁言");
                            return 0;
                        } else {
                            CQ.sendGroupMsg(fromGroup, "参数错误,正确用法: /muteall");
                            return 0;
                        }
                    } else {
                        CQ.sendGroupMsg(fromGroup, "你没有权限这样做");
                        return 0;
                    }
                case "-blacklist":
                    if (AdminConfig.getAdminList().contains(fromQQ) || fromQQ == masterQQ) {
                        if (str.length == 3) {
                            if (str[2].matches("[0-9]+")) {
                                switch (str[1]) {
                                    case "add":
                                        if (!(blackList.getBlacklist().contains(Long.parseLong(str[2])))) {
                                            CQ.sendGroupMsg(fromGroup, "正在处理中...");
                                            List<Long> list = blackList.getBlacklist();
                                            list.add(Long.parseLong(str[2]));
                                            blackList.setBlacklist(list);
                                            try {
                                                Yaml.dump(blackList, new File(CQ.getAppDirectory(), "blacklist.yml"));
                                            } catch (FileNotFoundException e) {
                                                CQ.sendPrivateMsg(masterQQ, "发生异常: FileNotFoundException \r\n详细信息: " + e.getMessage());
                                            }
                                            CQ.sendGroupMsg(fromGroup, "已成功将用户: " + str[2] + " 加入后端黑名单");
                                            if (hasUser(fromGroup, Long.parseLong(str[2]))) {
                                                CQ.setGroupKick(fromGroup, Long.parseLong(str[2]), false);
                                                CQ.sendGroupMsg(fromGroup, "检测到该人在此群，已自动踢出");
                                                List<Group> glist = CQ.getGroupList();
                                                for (int i = 0; i < glist.size(); i++) {
                                                    List<Member> mlist = CQ.getGroupMemberList(glist.get(i).getId());
                                                    for (int i2 = 0; i2 < mlist.size(); i2++) {
                                                        if (mlist.get(i2).getQQId() == Long.parseLong(str[2])) {
                                                            CQ.sendGroupMsg(mlist.get(i).getGroupId(), "检测到在黑名单内的成员: " + str[2] + " 已自动将其踢出");
                                                            CQ.setGroupKick(mlist.get(i).getGroupId(), Long.parseLong(str[2]), false);
                                                            break;
                                                        }
                                                    }
                                                }
                                            }
                                            return 0;
                                        } else {
                                            CQ.sendGroupMsg(fromGroup, "此人已经在黑名单中了！");
                                            return 0;
                                        }
                                    case "remove":
                                        if (blackList.getBlacklist().contains(Long.parseLong(str[2]))) {
                                            CQ.sendGroupMsg(fromGroup, "正在执行中...");
                                            List list = blackList.getBlacklist();
                                            for (int i = 0; i < list.size(); i++) {
                                                if (list.get(i).equals(Long.parseLong(str[2]))) {
                                                    list.remove(i);
                                                    break;
                                                }
                                            }
                                            blackList.setBlacklist(list);
                                            try {
                                                Yaml.dump(blackList, new File(CQ.getAppDirectory(), "blacklist.yml"));
                                            } catch (FileNotFoundException e) {
                                                CQ.sendPrivateMsg(masterQQ, "发生异常: FileNotFoundException \r\n详细信息: " + e.getMessage());
                                            }
                                            CQ.sendGroupMsg(fromGroup, "已成功从后端黑名单中删除此用户: " + str[2]);
                                            return 0;
                                        } else {
                                            CQ.sendGroupMsg(fromGroup, "黑名单中本来就没有此人！");
                                            return 0;
                                        }
                                    default:
                                        return 0;
                                }
                            } else {
                                switch (String.valueOf(CC.getAt(str[2]))) {
                                    case "-1":
                                        CQ.sendGroupMsg(fromGroup, "不能 @全体成员！");
                                        return 0;
                                    case "-1000":
                                        CQ.sendGroupMsg(fromGroup, "无效信息，请输入QQ号或 @某人");
                                        return 0;
                                    default:
                                        switch (str[1]) {
                                            case "add":
                                                if (!(blackList.getBlacklist().contains(CC.getAt(str[2])))) {
                                                    CQ.sendGroupMsg(fromGroup, "正在处理中...");
                                                    List<Long> list = blackList.getBlacklist();
                                                    list.add(CC.getAt(str[2]));
                                                    blackList.setBlacklist(list);
                                                    try {
                                                        Yaml.dump(blackList, new File(CQ.getAppDirectory(), "blacklist.yml"));
                                                    } catch (FileNotFoundException e) {
                                                        CQ.sendPrivateMsg(masterQQ, "发生异常: FileNotFoundException \r\n详细信息: " + e.getMessage());
                                                    }
                                                    CQ.sendGroupMsg(fromGroup, "已成功将用户: " + String.valueOf(CC.getAt(str[2])) + " 加入后端黑名单");
                                                    if (hasUser(fromGroup, CC.getAt(str[2]))) {
                                                        CQ.setGroupKick(fromGroup, CC.getAt(str[2]), false);
                                                        CQ.sendGroupMsg(fromGroup, "检测到该人在此群，已自动踢出");
                                                    }
                                                    List<Group> glist = CQ.getGroupList();
                                                    for (int i = 0; i < glist.size(); i++) {
                                                        List<Member> mlist = CQ.getGroupMemberList(glist.get(i).getId());
                                                        for (int i2 = 0; i2 < mlist.size(); i2++) {
                                                            if (mlist.get(i2).getQQId() == CC.getAt(str[2])) {
                                                                CQ.sendGroupMsg(mlist.get(i).getGroupId(), "检测到在黑名单内的成员: " + String.valueOf(CC.getAt(str[2])) + " 已自动将其踢出");
                                                                CQ.setGroupKick(mlist.get(i).getGroupId(), CC.getAt(str[2]), false);
                                                                break;
                                                            }
                                                        }
                                                    }

                                                    return 0;
                                                } else {
                                                    CQ.sendGroupMsg(fromGroup, "此人已经在黑名单中了！");
                                                    return 0;
                                                }
                                            case "remove":
                                                if (blackList.getBlacklist().contains(CC.getAt(str[2]))) {
                                                    CQ.sendGroupMsg(fromGroup, "正在执行中...");
                                                    List list1 = blackList.getBlacklist();
                                                    for (int i = 0; i < list1.size(); i++) {
                                                        if (list1.get(i).equals(CC.getAt(str[2]))) {
                                                            list1.remove(i);
                                                            break;
                                                        }
                                                    }
                                                    blackList.setBlacklist(list1);
                                                    try {
                                                        Yaml.dump(blackList, new File(CQ.getAppDirectory(), "blacklist.yml"));
                                                    } catch (FileNotFoundException e) {
                                                        CQ.sendPrivateMsg(masterQQ, "发生异常: FileNotFoundException \r\n详细信息: " + e.getMessage());
                                                    }
                                                    CQ.sendGroupMsg(fromGroup, "已成功从后端黑名单中删除此用户: " + String.valueOf(CC.getAt(str[2])));
                                                    return 0;
                                                } else {
                                                    CQ.sendGroupMsg(fromGroup, "黑名单中本来就没有此人！");
                                                    return 0;
                                                }
                                            default:
                                                return 0;
                                        }
                                }
                            }
                        } else {
                            CQ.sendGroupMsg(fromGroup, "参数错误，正确用法: -blacklist add [用户] \r\n-blacklist remove [用户]");
                            return 0;
                        }
                    } else {
                        CQ.sendGroupMsg(fromGroup, "你没有权限这样做");
                        return 0;
                    }
                case "/admin":
                    if (fromQQ == masterQQ) {
                        if (str.length == 3) {
                            if (str[2].matches("[0-9]+")) {
                                switch (str[1]) {
                                    case "add":
                                        if (AdminConfig.getAdminList().contains(Long.parseLong(str[2]))) {
                                            CQ.sendGroupMsg(fromGroup, "这个用户已经是管理员了");
                                            return 0;
                                        } else {
                                            List<Long> list = AdminConfig.getAdminList();
                                            list.add(Long.parseLong(str[2]));
                                            AdminConfig.setAdminList(list);
                                            try {
                                                Yaml.dump(AdminConfig, new File(CQ.getAppDirectory(), "admin.yml"));
                                            } catch (FileNotFoundException e) {
                                                CQ.sendPrivateMsg(masterQQ, "发生异常: FileNotFoundException \r\n详细信息: " + e.getMessage());
                                            }
                                            CQ.sendGroupMsg(fromGroup, "已成功将" + str[2] + "设为机器人的管理员");
                                            return 0;
                                        }
                                    case "remove":
                                        if (!(AdminConfig.getAdminList().contains(Long.parseLong(str[2])))) {
                                            CQ.sendGroupMsg(fromGroup, "这个人本来就不是机器人管理员");
                                            return 0;
                                        } else {
                                            List<Long> list = AdminConfig.getAdminList();
                                            for (int i = 0; i < list.size(); i++) {
                                                if (list.get(i).equals(Long.parseLong(str[2]))) {
                                                    list.remove(i);
                                                    break;
                                                }
                                            }
                                            try {
                                                Yaml.dump(AdminConfig, new File(CQ.getAppDirectory(), "admin.yml"));
                                            } catch (FileNotFoundException e) {
                                                CQ.sendPrivateMsg(masterQQ, "发生异常: FileNotFoundException \r\n详细信息: " + e.getMessage());
                                            }
                                            AdminConfig.setAdminList(list);
                                            CQ.sendGroupMsg(fromGroup, "成功取消此用户的管理员权限");
                                            return 0;
                                        }
                                    default:
                                        CQ.sendGroupMsg(fromGroup, "参数错误,正确用法: /admin add [用户] \r\n/admin remove [用户]");
                                        return 0;
                                }
                            } else {
                                switch (str[1]) {
                                    case "add":
                                        switch (String.valueOf(CC.getAt(str[2]))) {
                                            case "-1":
                                                CQ.sendGroupMsg(fromGroup, "不能给全体成员机器人管理员权限");
                                                return 0;
                                            case "-1000":
                                                CQ.sendGroupMsg(fromGroup, "无效信息，请输入QQ号或 @某人");
                                                return 0;
                                            default:
                                                if (AdminConfig.getAdminList().contains(CC.getAt(str[2]))) {
                                                    CQ.sendGroupMsg(fromGroup, "这个用户已经是管理员了");
                                                    return 0;
                                                } else {
                                                    List<Long> list = AdminConfig.getAdminList();
                                                    list.add(CC.getAt(str[2]));
                                                    AdminConfig.setAdminList(list);
                                                    try {
                                                        Yaml.dump(AdminConfig, new File(CQ.getAppDirectory(), "admin.yml"));
                                                    } catch (FileNotFoundException e) {
                                                        CQ.sendPrivateMsg(masterQQ, "发生异常: FileNotFoundException \r\n详细信息: " + e.getMessage());
                                                    }
                                                }
                                                CQ.sendGroupMsg(fromGroup, "已成功将 " + CC.at(CC.getAt(str[2])) + " 设为机器人的管理员");
                                                return 0;
                                        }
                                    case "remove":
                                        switch (String.valueOf(CC.getAt(str[2]))) {
                                            case "-1":
                                                CQ.sendGroupMsg(fromGroup, "不能取消全体成员的机器人管理员权限");
                                                return 0;
                                            case "-1000":
                                                CQ.sendGroupMsg(fromGroup, "无效信息，请输入QQ号或 @某人");
                                                return 0;
                                            default:
                                                if (!(AdminConfig.getAdminList().contains(CC.getAt(str[2])))) {
                                                    CQ.sendGroupMsg(fromGroup, "这个人本来就不是机器人管理员");
                                                    return 0;
                                                } else {
                                                    List<Long> list = AdminConfig.getAdminList();
                                                    for (int i = 0; i < list.size(); i++) {
                                                        if (list.get(i).equals(CC.getAt(str[2]))) {
                                                            list.remove(i);
                                                            break;
                                                        }
                                                    }
                                                    AdminConfig.setAdminList(list);
                                                    try {
                                                        Yaml.dump(AdminConfig, new File(CQ.getAppDirectory(), "admin.yml"));
                                                    } catch (FileNotFoundException e) {
                                                        CQ.sendPrivateMsg(masterQQ, "发生异常: FileNotFoundException \r\n详细信息: " + e.getMessage());
                                                    }
                                                    CQ.sendGroupMsg(fromGroup, "成功取消此用户的管理员权限");
                                                    return 0;
                                                }
                                        }
                                    default:
                                        CQ.sendGroupMsg(fromGroup, "参数错误,正确用法: /admin add [用户] \r\n/admin remove [用户]");
                                        return 0;
                                }
                            }
                        } else {
                            CQ.sendGroupMsg(fromGroup, "参数错误,正确用法: /admin add [用户] \r\n/admin remove [用户]");
                            return 0;
                        }
                    } else {
                        CQ.sendGroupMsg(fromGroup, "你没有权限这样做");
                        return 0;
                    }
                case "/master":
                    if (groupList.getGrouplist().contains(fromGroup)) {
                        CQ.sendGroupMsg(fromGroup, loadMenu.MasterMenu.toString());
                        return 0;
                    } else {
                        return 0;
                    }
                case "/管理员":
                    if (groupList.getGrouplist().contains(fromGroup)) {
                        CQ.sendGroupMsg(fromGroup, loadMenu.AdminMenu.toString());
                        return 0;
                    } else {
                        return 0;
                    }
                case "/bc":
                    if (str.length == 2) {
                        if (AdminConfig.getAdminList().contains(fromQQ) || fromQQ == masterQQ) {
                            CQ.sendGroupMsg(fromGroup, "正在广播中，广播消息为: " + str[1] + " ...");
                            List<Group> list = CQ.getGroupList();
                            for (int i = 0; i < list.size(); i++) {
                                CQ.sendGroupMsg(list.get(i).getId(), "【公告】" + str[1]);
                            }
                            CQ.sendGroupMsg(fromGroup, "信息广播完成");
                            return 0;
                        } else {
                            CQ.sendGroupMsg(fromGroup, "你没有权限这样做");
                            return 0;
                        }
                    } else {
                        CQ.sendGroupMsg(fromGroup, "参数错误,正确用法: /bc [信息]");
                        return 0;
                    }
                case "/word":
                    if (groupList.getGrouplist().contains(fromGroup)) {
                        if (AdminConfig.getAdminList().contains(fromQQ) || fromQQ == masterQQ) {
                            if (str.length == 3) {
                                switch (str[1]) {
                                    case "add":
                                        if (!(word.getWord().contains(str[2]))) {
                                            List<String> list = word.getWord();
                                            list.add(str[2]);
                                            word.setWord(list);
                                            try {
                                                Yaml.dump(word, new File(CQ.getAppDirectory(), "word.yml"));
                                            } catch (FileNotFoundException e) {
                                                CQ.sendPrivateMsg(masterQQ, "发生异常: FileNotFoundException \r\n详细信息: " + e.getMessage());
                                            }
                                            CQ.sendGroupMsg(fromGroup, "成功添加违禁词: " + str[2]);
                                            return 0;
                                        } else {
                                            CQ.sendGroupMsg(fromGroup,"已经有这个违禁词了");
                                            return 0;
                                        }
                                    case "remove":
                                        if (word.getWord().contains(str[2])) {
                                            List<String> list1 = word.getWord();
                                            for (int i = 0; i < list1.size(); i++) {
                                                if (list1.get(i).equals(str[2])) {
                                                    list1.remove(i);
                                                    break;
                                                }
                                            }
                                            CQ.sendGroupMsg(fromGroup,"成功删除此违禁词");
                                            return 0;
                                        } else {
                                            CQ.sendGroupMsg(fromGroup,"本来就没有这个违禁词");
                                            return 0;
                                        }
                                    default:
                                        CQ.sendGroupMsg(fromGroup,"参数错误,正确用法: /word add [词语] \r\n/word remove [词语]");
                                        return 0;
                                }
                            } else {
                                if (str.length == 2 && str[1].equals("list")) {
                                    StringBuffer stringBuffer = new StringBuffer();
                                    stringBuffer.append("违禁词列表如下: \r\n");
                                    List<String> list = word.getWord();
                                    for (int i = 0;i < list.size();i++) {
                                        if (i == list.size() - 1) {
                                            stringBuffer.append(list.get(i));
                                        } else {
                                            stringBuffer.append(list.get(i) + "\r\n");
                                        }
                                    }
                                    CQ.sendGroupMsg(fromGroup,stringBuffer.toString());
                                    return 0;
                                } else {
                                    CQ.sendGroupMsg(fromGroup,"参数错误，正确用法: /word add [词语] \r\n/word remove [词语] \r\n/word list");
                                    return 0;
                                }
                            }
                        } else {
                            CQ.sendGroupMsg(fromGroup,"你没有权限这样做");
                            return 0;
                        }
                    }
                case "/bot":
                    if (groupList.getGrouplist().contains(fromGroup)) {
                        if (str.length == 2) {
                            if (str[1].equalsIgnoreCase("info")) {
                                CQ.sendGroupMsg(fromGroup,loadMenu.BotInfo.toString());
                                return 0;
                            }
                        }
                    }
                case "/娱乐系统":
                    if (groupList.getGrouplist().contains(fromGroup)) {
                        if (str.length == 1) {
                            CQ.sendGroupMsg(fromGroup,loadMenu.Entertainment.toString());
                            return 0;
                        }
                    }
                case "/便民系统":
                    if (groupList.getGrouplist().contains(fromGroup)) {
                        if (str.length == 1) {
                            CQ.sendGroupMsg(fromGroup,loadMenu.Convenience.toString());
                            return 0;
                        }
                    }
                case "/运行代码":
                    if (groupList.getGrouplist().contains(fromGroup)) {
                        if (str.length == 1) {
                            try {
                                CQ.sendGroupMsg(fromGroup,"====在线运行代码====\r\n" + CC.image(new File(CQ.getAppDirectory(),"runcode.png")) + "\r\n" + CC.image(new File(CQ.getAppDirectory(),"runcode02.png")));
                            } catch (IOException e) {
                                CQ.sendPrivateMsg(masterQQ, "发生异常: IOException \r\n详细信息: " + e.getMessage());
                            }
                            return 0;
                        }
                    }
                case "/语音合成":
                    if (groupList.getGrouplist().contains(fromGroup)) {
                        if (str.length == 1) {
                            try {
                                CQ.sendGroupMsg(fromGroup,"====在线合成语音====\r\n" + CC.image(new File(CQ.getAppDirectory(),"yyhc.png")));
                            } catch (IOException e) {
                                CQ.sendPrivateMsg(masterQQ, "发生异常: IOException \r\n详细信息: " + e.getMessage());
                            }
                            return 0;
                        }
                    }
                default:
                    return 0;
            }
        }
        return 0;
    }

    //讨论组消息
    @Override
    public int discussMsg(int subtype, int msgId, long fromDiscuss, long fromQQ, String msg, int font) {
        return 0;
    }

    //群文件上传
    @Override
    public int groupUpload(int subType, int sendTime, long fromGroup, long fromQQ, String file) {
        if (Enable == false) {
            return 0;
        }
        if (!(groupList.getGrouplist().contains(fromGroup))) {
            return 0;
        }
        GroupFile groupFile = CQ.getGroupFile(file);
        if (groupFile == null) {
            CQ.sendGroupMsg(fromGroup, "文件解析失败");
            return 0;
        }
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(CC.at(fromQQ) + "上传了一个文件\r\n");
        stringBuffer.append("文件名: " + groupFile.getName() + "\r\n");
        stringBuffer.append("长度: " + String.valueOf(groupFile.getSize()) + " B");
        CQ.sendGroupMsg(fromGroup, stringBuffer.toString());
        return 0;
    }

    //管理员变动
    @Override
    public int groupAdmin(int subtype, int sendTime, long fromGroup, long beingOperateQQ) {
        if (Enable == false) {
            return 0;
        }
        if (!(groupList.getGrouplist().contains(fromGroup))) {
            return 0;
        }
        if (subtype == 2) {
            CQ.sendGroupMsg(fromGroup, "恭喜这个人变成管理员了！" + CC.at(beingOperateQQ));
            return 0;
        }
        if (subtype == 1) {
            CQ.sendGroupMsg(fromGroup, "真可惜，这个人被取消管理员权限了... " + CC.at(beingOperateQQ));
            return 0;
        }
        return 0;
    }

    //群人员减少
    @Override
    public int groupMemberDecrease(int subtype, int sendTime, long fromGroup, long fromQQ, long beingOperateQQ) {
        if (Enable == false) {
            return 0;
        }
        if (!(groupList.getGrouplist().contains(fromGroup))) {
            return 0;
        }
        if (blackList.getBlacklist().contains(beingOperateQQ)) {
            return 0;
        }
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("有群员默默退群了呢...\r\n");
        stringBuffer.append("他的QQ是: " + String.valueOf(beingOperateQQ) + "\r\n");
        stringBuffer.append("随他而去吧");
        CQ.sendGroupMsg(fromGroup, stringBuffer.toString());
        return 0;
    }

    //群成员增加
    @Override
    public int groupMemberIncrease(int subtype, int sendTime, long fromGroup, long fromQQ, long beingOperateQQ) {
        if (Enable == false) {
            return 0;
        }
        if (!(groupList.getGrouplist().contains(fromGroup))) {
            return 0;
        }
        if (blackList.getBlacklist().contains(beingOperateQQ)) {
            CQ.sendGroupMsg(fromGroup, "警告: 检测到黑名单用户 " + String.valueOf(beingOperateQQ) + " 进入群聊，机器人已自动踢出此用户");
            CQ.setGroupKick(fromGroup, beingOperateQQ, false);
            return 0;
        }
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("有新人入群辣！\r\n");
        stringBuffer.append(CC.at(beingOperateQQ) + " 你是第 " + String.valueOf(CQ.getGroupMemberList(fromGroup).size()) + " 个进入本群的人\r\n");
        stringBuffer.append("不过也请你遵守群规哦！否则将会收到惩罚的呢！\r\n");
        stringBuffer.append("大家快欢迎他吧！输入 菜单 即可查看机器人的所有功能哦！");
        CQ.sendGroupMsg(fromGroup, stringBuffer.toString());
        return 0;
    }

    //好友添加成功
    @Override
    public int friendAdd(int subtype, int sendTime, long fromQQ) {
        return 0;
    }

    //好友添加
    @Override
    public int requestAddFriend(int subtype, int sendTime, long fromQQ, String msg, String responseFlag) {
        return 0;
    }

    //群申请
    @Override
    public int requestAddGroup(int subtype, int sendTime, long fromGroup, long fromQQ, String msg, String responseFlag) {
        if (Enable == false) {
            return 0;
        }
        if (!(groupList.getGrouplist().contains(fromGroup))) {
            return 0;
        }
        if (blackList.getBlacklist().contains(fromQQ)) {
            CQ.setGroupAddRequest(responseFlag, REQUEST_GROUP_ADD, REQUEST_REFUSE, "你在黑名单内");
            CQ.sendGroupMsg(fromGroup, "警告: 已检测到黑名单用户 " + String.valueOf(fromQQ) + " 加群，机器人已自动拒绝申请");
        }
        if (hasUser(fromGroup, fromQQ)) {
            CQ.setGroupKick(fromGroup, fromQQ, false);
        }
        return 0;
    }

    //禁言
    public int groupBan(int subType, int sendTime, long fromGroup, long fromQQ, long beingOperateQQ, long duration) {
        return 0;
    }

    public void reloadConfig() {
        File blacklist = new File(directory, "blacklist.yml");
        File grouplist = new File(directory, "grouplist.yml");
        File admin = new File(directory, "admin.yml");
        File menu = new File(directory, "menu.txt");
        File carte = new File(directory, "menuList.yml");
        File word = new File(directory, "word.yml");
        if (!(blacklist.exists())) {
            try {
                blacklist.createNewFile();
            } catch (IOException e) {
                CQ.sendPrivateMsg(masterQQ, "发生异常: IOException \r\n详细信息: " + e.getMessage());
            }
        }
        if (!(grouplist.exists())) {
            try {
                grouplist.createNewFile();
            } catch (IOException e) {
                CQ.sendPrivateMsg(masterQQ, "发生异常: IOException \r\n详细信息: " + e.getMessage());
            }
        }
        if (!(admin.exists())) {
            try {
                admin.createNewFile();
            } catch (IOException e) {
                CQ.sendPrivateMsg(masterQQ, "发生异常: IOException \r\n详细信息: " + e.getMessage());
            }
        }
        if (!(menu.exists())) {
            try {
                menu.createNewFile();
            } catch (IOException e) {
                CQ.sendPrivateMsg(masterQQ, "发生异常: IOException \r\n详细信息: " + e.getMessage());
            }
        }
        if (!(carte.exists())) {
            try {
                carte.createNewFile();
            } catch (IOException e) {
                CQ.sendPrivateMsg(masterQQ, "发生异常: IOException \r\n详细信息: " + e.getMessage());
            }
        }
        if (!(word.exists())) {
            try {
                word.createNewFile();
            } catch (IOException e) {
                CQ.sendPrivateMsg(masterQQ, "发生异常: IOException \r\n详细信息: " + e.getMessage());
            }
        }


        BlackList blackList = new BlackList();
        if (blacklist.length() == 0) {
            List<Long> list = new ArrayList<Long>();
            list.add(123456789L);
            blackList.setBlacklist(list);
            try {
                Yaml.dump(blackList, blacklist, true);
            } catch (FileNotFoundException e) {
                CQ.sendPrivateMsg(masterQQ, "发生异常: FileNotFoundException \r\n详细信息: " + e.getMessage());
            }
        }
        try {
            blackList = Yaml.loadType(blacklist, BlackList.class);
        } catch (FileNotFoundException e) {
            CQ.sendPrivateMsg(masterQQ, "发生异常: FileNotFoundException \r\n详细信息: " + e.getMessage());
        }
        this.blackList = blackList;

        Admin adminConfig = new Admin();
        if (admin.length() == 0) {
            List<Long> list = new ArrayList<Long>();
            list.add(123456789L);
            adminConfig.setAdminList(list);
            try {
                Yaml.dump(adminConfig, admin, true);
            } catch (FileNotFoundException e) {
                CQ.sendPrivateMsg(masterQQ, "发生异常: FileNotFoundException \r\n详细信息: " + e.getMessage());
            }
        }
        try {
            adminConfig = Yaml.loadType(admin, Admin.class);
        } catch (FileNotFoundException e) {
            CQ.sendPrivateMsg(masterQQ, "发生异常: FileNotFoundException \r\n详细信息: " + e.getMessage());
        }
        AdminConfig = adminConfig;

        GroupList groupList2 = new GroupList();
        if (grouplist.length() == 0) {
            List<Long> list = new ArrayList<Long>();
            list.add(123456789L);
            groupList2.setGrouplist(list);
            try {
                Yaml.dump(groupList2, grouplist, true);
            } catch (FileNotFoundException e) {
                CQ.sendPrivateMsg(masterQQ, "发生异常: FileNotFoundException \r\n详细信息: " + e.getMessage());
            }
        }
        try {
            groupList2 = Yaml.loadType(grouplist, GroupList.class);
        } catch (FileNotFoundException e) {
            CQ.sendPrivateMsg(masterQQ, "发生异常: FileNotFoundException \r\n详细信息: " + e.getMessage());
        }
        groupList = groupList2;

        Word word1 = new Word();
        if (word.length() == 0) {
            List<String> list = new ArrayList<String>();
            list.add("傻逼");
            word1.setWord(list);
            try {
                Yaml.dump(word1, word, true);
            } catch (FileNotFoundException e) {
                CQ.sendPrivateMsg(masterQQ, "发生异常: FileNotFoundException \r\n详细信息: " + e.getMessage());
            }
        }
        try {
            word1 = Yaml.loadType(word, Word.class);
        } catch (FileNotFoundException e) {
            CQ.sendPrivateMsg(masterQQ, "发生异常: FileNotFoundException \r\n详细信息: " + e.getMessage());
        }
        this.word = word1;

        menuList mlist = new menuList();
        if (carte.length() == 0) {
            Field[] mfields = new menuList().getClass().getDeclaredFields();
            for (int i = 0; i < mfields.length; i++) {
                Field field = mfields[i];
                field.setAccessible(true);
                List<String> list = new ArrayList<>();
                list.add("test");
                try {
                    field.set(mlist, list);
                } catch (IllegalAccessException e) {
                    CQ.sendPrivateMsg(masterQQ, "发生异常: IllegalAccessException \r\n详细信息: " + e.getMessage());
                }
            }
            try {
                Yaml.dump(mlist, carte, true);
            } catch (FileNotFoundException e) {
                CQ.sendPrivateMsg(masterQQ, "发生异常: FileNotFoundException \r\n详细信息: " + e.getMessage());
            }
        }
        try {
            mlist = Yaml.loadType(carte, menuList.class);
        } catch (IOException e) {
            CQ.sendPrivateMsg(masterQQ, "发生异常: IOException \r\n详细信息: " + e.getMessage());
        }
        new loadMenu(mlist.getGroupManager(), mlist.getEntertainment(), mlist.getConvenience(), mlist.getMasterMenu(), mlist.getAdminMenu(), mlist.getBotInfo());
        this.menulist = mlist;

        try {
            FileInputStream fileInputStream = new FileInputStream(new File(CQ.getAppDirectory(), "menu.txt"));
            byte[] bytes = new byte[1024];
            int temp = 0;
            StringBuffer stringBuffer = new StringBuffer();
            while ((temp = fileInputStream.read(bytes)) != -1) {
                stringBuffer.append(new String(bytes, 0, temp));
            }
            fileInputStream.close();
            menuStringBuffer = stringBuffer;
        } catch (FileNotFoundException e) {
            CQ.sendPrivateMsg(masterQQ, "发生异常: FileNotFoundException \r\n详细信息: " + e.getMessage());
        } catch (IOException e) {
            CQ.sendPrivateMsg(masterQQ, "发生异常: IOException \r\n详细信息: " + e.getMessage());
        }

    }

    public boolean hasUser(long fromGroup, long QQ) {
        List<Member> list = CQ.getGroupMemberList(fromGroup);
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getQQId() == QQ) {
                return true;
            }
        }
        return false;
    }

    public void setInstance(bot b) {
        instance = b;
    }

    public bot getInstance() {
        return instance;
    }

}

